/**
 * hook_defs.h - 钩子函数原型定义
 *
 * 此文件包含钩子框架中使用的函数原型和数据结构。
 */

#ifndef HOOK_DEFS_H
#define HOOK_DEFS_H

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>
#include <time.h>
#include <sys/time.h>
#include <sys/utsname.h>
#include <openssl/ssl.h>
#include <netdb.h>
#include <errno.h>

/* 系统I/O函数指针类型 */
typedef ssize_t (*orig_read_t)(int fd, void *buf, size_t count);
typedef ssize_t (*orig_write_t)(int fd, const void *buf, size_t count);
typedef ssize_t (*orig_send_t)(int sockfd, const void *buf, size_t len, int flags);
typedef ssize_t (*orig_recv_t)(int sockfd, void *buf, size_t len, int flags);

/* 文件系统函数指针类型 */
typedef int (*orig_open_t)(const char *pathname, int flags, ...);
typedef int (*orig_close_t)(int fd);
typedef FILE* (*orig_fopen_t)(const char *pathname, const char *mode);
typedef int (*orig_fclose_t)(FILE *stream);

/* 网络函数指针类型 */
typedef int (*orig_connect_t)(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
typedef int (*orig_accept_t)(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
typedef int (*orig_socket_t)(int domain, int type, int protocol);

/* SSL函数指针类型（如果启用了SSL支持） */
#ifdef ENABLE_SSL_HOOKS
typedef int (*orig_ssl_read_t)(SSL *ssl, void *buf, int num);
typedef int (*orig_ssl_write_t)(SSL *ssl, const void *buf, int num);
typedef int (*orig_ssl_connect_t)(SSL *ssl);
typedef int (*orig_ssl_accept_t)(SSL *ssl);
#endif

/* 文件I/O钩子 */
int open(const char *pathname, int flags, ...);
int close(int fd);
ssize_t read(int fd, void *buf, size_t count);
ssize_t write(int fd, const void *buf, size_t count);
FILE *fopen(const char *pathname, const char *mode);
int fclose(FILE *stream);
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
int stat(const char *pathname, struct stat *statbuf);
int lstat(const char *pathname, struct stat *statbuf);
int fstat(int fd, struct stat *statbuf);
int mkdir(const char *pathname, mode_t mode);
int rmdir(const char *pathname);
int unlink(const char *pathname);

/* 网络钩子 */
int socket(int domain, int type, int protocol);
int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
int listen(int sockfd, int backlog);
int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
ssize_t send(int sockfd, const void *buf, size_t len, int flags);
ssize_t recv(int sockfd, void *buf, size_t len, int flags);
ssize_t sendto(int sockfd, const void *buf, size_t len, int flags,
               const struct sockaddr *dest_addr, socklen_t addrlen);
ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags,
                struct sockaddr *src_addr, socklen_t *addrlen);
struct hostent *gethostbyname(const char *name);
int getaddrinfo(const char *node, const char *service,
               const struct addrinfo *hints, struct addrinfo **res);

/* 进程钩子 */
pid_t fork(void);
pid_t vfork(void);
int clone(int (*fn)(void *), void *child_stack, int flags, void *arg, ...);
int execve(const char *pathname, char *const argv[], char *const envp[]);
int execl(const char *pathname, const char *arg, ...);
int execlp(const char *file, const char *arg, ...);
int execle(const char *pathname, const char *arg, ...);
int execv(const char *pathname, char *const argv[]);
int execvp(const char *file, char *const argv[]);
int system(const char *command);
pid_t getpid(void);
pid_t getppid(void);
uid_t getuid(void);
uid_t geteuid(void);

/* 内存钩子 */
void *malloc(size_t size);
void *calloc(size_t nmemb, size_t size);
void *realloc(void *ptr, size_t size);
void free(void *ptr);
void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset);
int munmap(void *addr, size_t length);
int brk(void *addr);
void *sbrk(intptr_t increment);

/* 加密钩子 */
int SSL_read(SSL *ssl, void *buf, int num);
int SSL_write(SSL *ssl, const void *buf, int num);
int SSL_connect(SSL *ssl);
int SSL_accept(SSL *ssl);
SSL_CTX *SSL_CTX_new(const SSL_METHOD *method);
int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str);
int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type);
int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type);

/* 系统钩子 */
char *getenv(const char *name);
int setenv(const char *name, const char *value, int overwrite);
int unsetenv(const char *name);
time_t time(time_t *tloc);
int gettimeofday(struct timeval *__restrict tv, void *__restrict tz);
int clock_gettime(clockid_t clk_id, struct timespec *tp);
int uname(struct utsname *buf);
int gethostname(char *name, size_t len);

/* 钩子框架内部函数 */
void handle_socket_close(int fd);
int load_plugins_from_env(void);

#endif /* HOOK_DEFS_H */
